// store/notification.js
import { defineStore } from 'pinia'

export const useNotificationStore = defineStore('notification', {
    state: () => ({
        notifications: [],
        unreadCount: 0
    }),

    getters: {
        // 获取未读通知数量
        getUnreadCount: (state) => {
            return state.notifications.filter(n => !n.isRead).length
        },

        // 获取所有通知，按创建时间倒序
        getAllNotifications: (state) => {
            return [...state.notifications].sort((a, b) =>
                new Date(b.createdAt) - new Date(a.createdAt)
            )
        },

        // 获取未读通知
        getUnreadNotifications: (state) => {
            return state.notifications.filter(n => !n.isRead)
                .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
        },

        // 获取评估报告类型的通知
        getEvaluationReports: (state) => {
            return state.notifications.filter(n => n.type === 'evaluation_report')
                .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
        }
    },

    actions: {
        // 添加通知
        addNotification(notification) {
            const newNotification = {
                id: notification.id || `notif_${Date.now()}`,
                type: notification.type || 'general',
                title: notification.title || '新通知',
                message: notification.message || '',
                reportId: notification.reportId || null,
                modelUrl: notification.modelUrl || null,
                isRead: notification.isRead || false,
                createdAt: notification.createdAt || new Date().toISOString()
            }

            // 检查是否已存在相同ID的通知
            const existingIndex = this.notifications.findIndex(n => n.id === newNotification.id)
            if (existingIndex !== -1) {
                // 更新现有通知
                this.notifications[existingIndex] = newNotification
            } else {
                // 添加新通知
                this.notifications.unshift(newNotification)
            }

            // 限制通知数量（最多保留100条）
            if (this.notifications.length > 100) {
                this.notifications = this.notifications.slice(0, 100)
            }

            console.log('[通知] 添加通知:', newNotification)
        },

        // 标记通知为已读
        markAsRead(notificationId) {
            const notification = this.notifications.find(n => n.id === notificationId)
            if (notification) {
                notification.isRead = true
                console.log('[通知] 标记为已读:', notificationId)
            }
        },

        // 标记所有通知为已读
        markAllAsRead() {
            this.notifications.forEach(notification => {
                notification.isRead = true
            })
            console.log('[通知] 所有通知已标记为已读')
        },

        // 删除通知
        removeNotification(notificationId) {
            const index = this.notifications.findIndex(n => n.id === notificationId)
            if (index !== -1) {
                this.notifications.splice(index, 1)
                console.log('[通知] 删除通知:', notificationId)
            }
        },

        // 清空所有通知
        clearAllNotifications() {
            this.notifications = []
            console.log('[通知] 清空所有通知')
        },

        // 获取通知详情
        getNotificationById(notificationId) {
            return this.notifications.find(n => n.id === notificationId)
        },

        // 更新通知
        updateNotification(notificationId, updates) {
            const notification = this.notifications.find(n => n.id === notificationId)
            if (notification) {
                Object.assign(notification, updates)
                console.log('[通知] 更新通知:', notificationId, updates)
            }
        }
    },

    // 持久化配置
    persist: {
        key: 'notification-store',
        storage: localStorage,
        paths: ['notifications'] // 只持久化通知数据
    }
})